package com.ad.pet.app.account;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ad.pet.DaggerMainActivityCompent;
import com.ad.pet.R;
import com.ad.pet.app.Navigation;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.AppToolbarActivity;
import com.ad.pet.models.remote.account.AboutUs;
import com.ad.pet.utils.LyxUtils;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liuyixiang on 2019/10/22.
 */

public class AboutUsActivity extends AppToolbarActivity implements AboutUsContract.View {
    @BindView(R.id.tv_private)
    TextView tvPrivate;
    @BindView(R.id.tv_xieyi)
    TextView tvXieyi;
    @Inject
    AboutUsPresenter aboutUsPresenter;
    @BindView(R.id.tv_icon)
    ImageView tvIcon;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.tv_phone)
    TextView tvPhone;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mStatusLayoutManager.showContent();
        mToolbar.setTitle(getString(R.string.about_86));
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_aboutus;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    protected void initViews() {
        tvXieyi.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tvXieyi.getPaint().setAntiAlias(true);//抗锯齿
        tvPrivate.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tvPrivate.getPaint().setAntiAlias(true);//抗锯齿
        aboutUsPresenter.onCreate(this);
    }

    @Override
    protected void initData() {
        tvVersion.setText("当当DOG v" + LyxUtils.getVersionName());
        aboutUsPresenter.aboutUs();
    }

    @Override
    protected void initEvents() {
        tvXieyi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.goCommonWebView(mActivity, "", getString(R.string.ip_host)+"/index.html#/service/ServicesAgreement");
            }
        });
        tvPrivate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.goCommonWebView(mActivity, "", getString(R.string.ip_host)+"/index.html#/service/PrivacyPolicy");
            }
        });
    }

    @Override
    public void initInject() {
        DaggerMainActivityCompent.builder().applicationComponent(getApplicationComponent())

                .activityModule(getActivityModule()).build().inject(this);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void error(int code, String msg) {

    }

    @Override
    public void aboutUsSuccess(final AboutUs aboutUs) {
        tvPhone.setText("客服热线:" + aboutUs.getWeb_phone());
        tvPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                call_phone(aboutUs.getWeb_phone());
            }
        });
    }

    @Override
    public void sendSuggessSuccess() {

    }

    @Override
    public void clearAccountSuccess() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    public void call_phone(String phone) {


        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) this, Manifest.permission.CALL_PHONE)) {

                Toast.makeText(this, "请授权！", Toast.LENGTH_LONG).show();

                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", this.getPackageName(), null);
                intent.setData(uri);
                this.startActivity(intent);
            } else {
                ActivityCompat.requestPermissions((Activity) this, new String[]{Manifest.permission.CALL_PHONE}, 1);
            }
        } else {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:" + phone));
            this.startActivity(intent);
        }

    }
}
